<template>
    <div>
        <div class="login_content">
            <div class="login_project_name_one">{{ systemName }}</div>
            <div class="login_project_name_two">({{ systemSubtitle }})</div>
            <el-card>
                <div class="login_content_title">用户登录</div>
                <div class="login_edit_input">
                    <el-input v-model="phoneNumber" class="login_input" placeholder="手机号/账号" @keydown.enter.native="onLoginIn"></el-input>
                </div>
                <div class="login_edit_input">
                    <el-input v-model="password" class="login_input" type="password" placeholder="密码" @keydown.enter.native="onLoginIn"></el-input>
                </div>
                <!--                <div title="点击刷新" @click="getRandomCode" class="qr-bg">-->
                <!--                    <div v-for="(item,index) in lineList" class="line"-->
                <!--                         :style="{width:item.width,background:item.background,left:item.left,transform:item.transform}"></div>-->
                <!--                    <div class="num" v-for="(item,index) in list" :key="index"-->
                <!--                         :style="{transform:item.transform,color:item.color}">{{item.num}}-->
                <!--                    </div>-->
                <!--                </div>-->
                <!--                <div class="login_edit_input">-->
                <!--                    <div class="login_sms_input">-->
                <!--                        <el-input v-model="code" maxlength="4" placeholder="请输入" style="width: 200px;"></el-input>-->
                <!--                    </div>-->
                <!--                    <div class="login_sms_input">-->
                <!--                        <el-button :class="smsFlag?'el-button_false':'el-button_true'" @click="sendCode" :disabled="smsFlag" >{{ countdown }}</el-button>-->
                <!--                    </div>-->
                <!--                </div>-->
                <!--                <div class="login_edit_input">-->
                <!--                    <el-input v-model="smsCode" class="login_input" placeholder="验证码" @keydown.enter.native="onLoginIn"></el-input>-->
                <!--                </div>-->
                <div class="login_edit_button">
                    <el-button v-loading.fullscreen.lock="loading" @click="onLoginIn">登 录</el-button>
                </div>
            </el-card>
        </div>
    </div>
</template>

<script>

export default {
    name: "login",
    data() {
        return {
            loading: false,
            baseURL: '',
            phoneNumber: '',
            password: '',
            smsCode: '',
            randomCode: '',
            code: '',
            smsFlag: false,
            countdown: '发送短信',
            countdownN: 60,

            list: [],
            lineList: [],

            systemName: CONFIG.systemName,
            systemSubtitle: CONFIG.systemSubtitle,
        }
    },
    mounted() {
        this.getRandomCode();
    },
    methods: {
        getRandomCode() {
            var str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
            var arr = str.split("");
            let list = [], lineList = [];
            var result = "";
            for (var i = 0; i < 4; i++) {
                var n = Math.floor(Math.random() * arr.length);
                result += arr[n];
                list.push({
                    num: arr[n],
                    transform: 'rotate(' + (-45 * Math.random() + 45) + 'deg)',
                    color: 'rgb(' + Math.floor(Math.random() * 256) + ',' + Math.floor(Math.random() * 256) + ',' + Math.floor(Math.random() * 256) + ')'
                });
            }
            for(var i = 0; i < Math.floor(Math.random() * 60 + 1);i++){
                lineList.push({
                    width: Math.random() * 200 + 'px',
                    left: Math.random() * 200 + 'px',
                    transform: 'rotate(' + (360 * Math.random()) + 'deg)',
                    background: 'rgb(' + Math.floor(Math.random() * 256) + ',' + Math.floor(Math.random() * 256) + ',' + Math.floor(Math.random() * 256) + ')',
                })
            }

            this.list = list;
            console.log(this.list)
            this.lineList = lineList;
            console.log(this.lineList)
            this.randomCode = result;
            console.log(this.randomCode)
        },
        sendCode() {
            if (this.code === '') {
                return this.$message.error('请输入验证码')
            }
            if (this.code.toLocaleLowerCase() == this.randomCode.toLocaleLowerCase()) {
                this.$axios.post("/login/sendCode", {
                    phoneNumber: this.phoneNumber,
                    code: this.code,
                }).then(res => {
                    this.beginDelayed();
                    this.$message.success("验证码发送成功！");
                }).catch(err => {
                    this.$message.error(err.message);
                })
            } else {
                this.$message.error('验证码输入错误！')
            }

        },
        onLoginIn() {
            if (this.phoneNumber == '') {
                return this.$message.error('用户名不能为空')
            }
            if (this.password == '') {
                return this.$message.error('密码不能为空')
            }
            this.loading = true;
            this.$axios.post("/login/loginIn", {
                phoneNumber: this.phoneNumber,
                password: this.password
            }).then(res => {
                let data = res.data;
                this.$root.setStorage('authorization', data.token || '');
                this.$root.setStorage('userId', data.id);
                this.$root.setStorage('nickName', data.nickName);
                this.$root.setStorage('userData', data);
                this.$router.replace('/index');
                this.loading = false;
            }).catch(err => {
                this.$message.error(err.message);
                this.loading = false;
            })
        },
        beginDelayed(){
            this.smsFlag = true;
            this.countdownN = 60;
            this.countdown = '等待' + this.countdownN + "秒";
            this.timer = setInterval(()=>{
                if (this.countdown == 0) {
                    this.smsFlag = false;
                    this.countdown = '发送短信';
                } else {
                    this.countdownN--;
                    this.countdown = '等待' + this.countdownN + "秒";
                }
            },1000)
        },
    }
}
</script>

<style lang="scss">

.login_content {
    position: relative;
    background-image: url("../common/img/login.jpg");
    width: 100%;
    height: calc(100vh);

    .login_project_name_one {
        width: 100%;
        height: 60px;
        line-height: 60px;
        font-size: 36px;
        text-align: center;
        position: absolute;
        top: 50%;
        margin-top: -300px;
    }

    .login_project_name_two {
        width: 100%;
        height: 60px;
        line-height: 60px;
        font-size: 20px;
        text-align: center;
        position: absolute;
        top: 50%;
        margin-top: -240px;
    }

    .el-card {
        width: 400px;
        position: absolute;
        left: 50%;
        top: 50%;
        margin-left: -200px;
        margin-top: -140px;

        .login_content_title {
            height: 40px;
            line-height: 40px;
            font-size: 24px;
            text-align: center;
        }

        .login_edit_input {
            height: 40px;
            line-height: 40px;
            margin: 20px;
        }

        .login_sms_input{
            margin-right: 10px;
            float: left;

            .el-button {
                padding: 0 20px;
                height: 40px;
                line-height: 40px;
                color: white;
            }

            .el-button_true {
                padding: 0 20px;
                height: 40px;
                line-height: 40px;
                background-color: #409EFF;
                color: white;
            }

            .el-button_false {
                padding: 0 20px;
                height: 40px;
                line-height: 40px;
                background-color: #a0a0a0;
                color: white;
            }

            .el-button:hover {
                background-color: #66b1ff;
                color: white;
            }
        }

        .login_edit_button {
            margin: 20px;

            .el-button {
                height: 40px;
                line-height: 40px;
                background-color: #409EFF;
                color: white;
                width: 100%;
            }

            .el-button:hover {
                background-color: #66b1ff;
                color: white;
            }
        }
    }
}

.qr-bg {
    margin: 20px;
    padding: 0 20px;
    background: #a0a0a0;
    color: black;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    user-select:none;
    .num {
        width: 40ppx;
        height: 60px;
        font-size: 60px!important;
        line-height: 60px;
        text-shadow: #ccc 1px 0 0, #ccc 0 1px 0, #ccc 1px 0 0, #ccc 0 1px 0;
        float: left;
        margin: 10px;
        user-select:none;
    }
    .line{
        height: 1px;
        position: absolute;
        user-select:none;
    }
}

</style>